<?php

require_once "utils/Rest.inc.php";
require_once "utils/Constants.php";

abstract class DatabaseManager extends REST{
	
	// DataBase Constants
	const DB_SERVER = "127.0.0.1:8889";	
	const DB_USER = "root";
	const DB_PASSWORD = "root";
	const DB_NAME = "bookings_managing";
	
	// DataBase Connection
	protected $connection;
	
	function __construct() {
        parent::__construct();
		if (!$this->openConnection()) {
			// Error connectiong to the database	
			$jsonResults[MESSAGE] = ErrorMessages::DATA_BASE_ERROR;
			$this->response($this->toJSON($jsonResults), StatusCodes::STATUS_CODE_SERVER_ERROR); 	
		} 	
	}
	
	// This function opens a connection
	protected function openConnection() {
		$this->connection = mysql_connect(DatabaseManager::DB_SERVER, DatabaseManager::DB_USER, DatabaseManager::DB_PASSWORD);
		if ($this->connection) {
			mysql_select_db(DatabaseManager::DB_NAME);
			return true;	
		}
		return false;
	}
	
	// This function closes a connection
	protected function closeConnection() {
		if (isset($this->connection)) {
			mysql_close($this->connection);
		}
	}
	
	// Each subclass will parse the query string
	abstract public function processApi();
	
	
	// Destructor closes the connection
	function __destruct () {
		$this->closeConnection();	
	}
}

?>